package pcloud.server.datastore;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

import pcloud.server.Logger;

public class FileSystemTaskList implements ClientTaskList
{
	private String clientId;
	private Properties tasks = null;
	private String tasklistFolderPath;

	public FileSystemTaskList(String id, String datastoreRootPath)
	{
		clientId = id;
		tasklistFolderPath = datastoreRootPath + "/"+clientId;
	}

	/* (non-Javadoc)
	 * @see pcloud.server.datastore.ClientTaskList#updateTask(java.lang.String, java.lang.String)
	 */
	@Override
	public boolean updateTask(String path, String action)
	{
		try {
			tasks.setProperty(path, action);
			Logger.log("\tbefore save task path:"+path+", action: "+action);
			
			saveTask(path, action);
		} catch (IOException e) {
			Logger.log("\tafter save task return: false");
			return false;
		}
		Logger.log("\tafter save task return: true");
		return true;
	}

	private void saveTask(String path, String action) throws IOException
	{
		File taskFile = new File(tasklistFolderPath+"/"+convertPath2FileName(path));
		if (taskFile.exists()) {
			taskFile.createNewFile();
		}
		Logger.log("\tUpdate task file: ["+taskFile.getName()+"], action: "+action);
		BufferedWriter writer = new BufferedWriter( new FileWriter(taskFile) );
		writer.write(action);
		writer.close();
	}


	private Properties getTasks()
	{
		if (tasks != null) {
			return tasks;
		}
		
		tasks = new Properties();
		
		File tasklistFolder = new File(tasklistFolderPath);
		try {
			if (!tasklistFolder.exists()) {
				tasklistFolder.mkdir();
			}
			File[] taskFiles = tasklistFolder.listFiles();
			for (File taskFile : taskFiles) {
				BufferedReader reader = new BufferedReader( new FileReader(taskFile) );
				String path = convertFileName2Path(taskFile.getName());
				String action = reader.readLine();
				reader.close();
				tasks.put(path, action);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tasks;
	}

	private String convertPath2FileName(String path)
	{
		return path==null ? null : path.replace("/", "~_~");
	}

	private String convertFileName2Path(String path)
	{
		return path==null ? null : path.replace("~_~", "/");
	}

	/* (non-Javadoc)
	 * @see pcloud.server.datastore.ClientTaskList#removeTask(java.lang.String, java.lang.String)
	 */
	@Override
	public boolean removeTask(String path, String action)
	{
		tasks.remove(path);
		File taskFile = new File(tasklistFolderPath+convertPath2FileName(path));
		if (taskFile.exists()) {
			BufferedReader reader;
			try {
				reader = new BufferedReader( new FileReader(taskFile) );
				String actionInFile = reader.readLine();
				if (actionInFile.equals(action)) {
					taskFile.delete();
					return true;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return false;
	}



	/* (non-Javadoc)
	 * @see pcloud.server.datastore.ClientTaskList#size()
	 */
	@Override
	public int size ()
	{
		return getTasks().size();
	}
}
